﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ExamCommon;
using ExamClientBLL;
using HZH_Controls.Forms;
using System.Runtime.InteropServices;
using Exam_Client.childForms;

namespace Exam_Client
{
    public partial class FrmMainClient : Form
    {
        public FrmMainClient()
        {
            InitializeComponent();
            openChildForm(GenerateSingletonFrm<FrmHome>.CreateInstrance());
        }

        //拖动控件窗体
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        //拖动窗体
        private void panelTitleBar_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        public void ShowLoginSeccessTips()
        {
            FrmTips.ShowTips(this, "登录成功", 2000, false, ContentAlignment.TopCenter, null, TipsSizeMode.Small, null, TipsState.Success);
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btn_min_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void btn_max_Click(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Maximized)
            {
                WindowState = FormWindowState.Normal;
            }
            else
            {   //最大化不会覆盖系统状态栏
                MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                WindowState = FormWindowState.Maximized;
            }
        }

        private void btn_close_MouseEnter(object sender, EventArgs e)
        {
            this.btn_close.BackColor = Color.Red;
        }

        private void btn_close_MouseLeave(object sender, EventArgs e)
        {
            this.btn_close.BackColor = Color.FromArgb(38, 87, 188);
        }

        // 打开子窗体
        private void openChildForm(Form childForm)
        {
            foreach (Control item in this.panel_Desktop.Controls)
            {
                if (item is Form)
                {
                    if (childForm == item)
                    {
                        break;
                    }
                    else
                    {
                        Form frm = (Form)item;
                        frm.Close();
                        frm.Dispose();
                        frm = null;
                    }
                }
            }
            childForm.TopLevel = false; // 设置非顶级控件
            childForm.Parent = this.panel_Desktop; // 父级容器
            childForm.FormBorderStyle = FormBorderStyle.None; // 去除边框
            childForm.Dock = DockStyle.Fill; // 全覆盖
            childForm.Show();
        }

        Button tempButton = null;
        // 选中后更改按钮颜色
        private void changeButtonBackColor(Button button)
        {
            if (tempButton != null)
            {
                tempButton.BackColor = Color.FromArgb(238, 238, 246);
            }
            if (button != null)
            {
                tempButton = button;
                tempButton.BackColor = Color.FromArgb(220, 214, 230);
            }
        }

        private void btn_onlineExam_Click(object sender, EventArgs e)
        {
            openChildForm(GenerateSingletonFrm<FrmOnlineExam>.CreateInstrance());
            changeButtonBackColor(btn_onlineExam);
        }

        private void btn_achievement_Click(object sender, EventArgs e)
        {
            openChildForm(GenerateSingletonFrm<FrmAchievement>.CreateInstrance());
            changeButtonBackColor(btn_achievement);
        }

        private void btn_exercise_Click(object sender, EventArgs e)
        {
            openChildForm(GenerateSingletonFrm<FrmExercise>.CreateInstrance());
            changeButtonBackColor(btn_exercise);
        }

        private void btn_personalInfo_Click(object sender, EventArgs e)
        {
            openChildForm(GenerateSingletonFrm<FrmPersonalInfo>.CreateInstrance());
            changeButtonBackColor(btn_personalInfo);
        }

        private void pictureBox_logo_Click(object sender, EventArgs e)
        {
            openChildForm(GenerateSingletonFrm<FrmHome>.CreateInstrance());
            changeButtonBackColor(null);
        }
    }
}
